package com.study.service;

import com.study.dao.EcommerceUserDao;
import com.study.entity.EcommerceUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;

@Slf4j
@Service
public class TransactionalService {

    @Autowired
    private EcommerceUserDao userDao;


    @Transactional
    public void wrongTest001() throws Exception {
        EcommerceUser user = new EcommerceUser();
        user.setUsername("2022-12-04");
        user.setPassword("25d55ad283aa400af464c76d713c07ad");
        user.setExtraInfo("{}");
        userDao.save(user);//save方法执行了

        throw new IOException("异常。。。");//默认事务、不回滚
    }

    //同一个类中方法调用
    public void wrongTest002() throws Exception {
        this.rightTest001();//save方法执行了
        //没有事务、不回滚
    }

    public void wrongTest003() throws Exception {
        EcommerceUser user = new EcommerceUser();
        user.setUsername("2022-12-04");
        user.setPassword("25d55ad283aa400af464c76d713c07ad");
        user.setExtraInfo("{}");
        userDao.save(user);//save方法执行了
        try {
            throw new IOException("异常。。。");//异常、catch、没有事务不回滚
        }catch (Exception e){
            log.error("异常：[]",e.getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void wrongTest004() throws Exception {
        EcommerceUser user = new EcommerceUser();
        user.setUsername("2022-12-04");
        user.setPassword("25d55ad283aa400af464c76d713c07ad");
        user.setExtraInfo("{}");
        userDao.save(user);//save方法执行了
        try {
            throw new IOException("异常。。。");//catch异常了、事务失效、不回滚
        }catch (Exception e){
            log.error("异常：[]",e.getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void wrongTest005() throws Exception {
        try {
            EcommerceUser user = new EcommerceUser();
            user.setUsername("2022-12-04");
            user.setPassword("25d55ad283aa400af464c76d713c07ad");
            user.setExtraInfo("{}");
            userDao.save(user);//save方法执行了
            //throw new IOException("异常。。。");
            int i = 1 / 0;//catch异常了、事务失效、不回滚
        }catch (Exception e){
            log.error("异常：{}",e.getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void rightTest001() throws Exception {
        EcommerceUser user = new EcommerceUser();
        user.setUsername("2022-12-04");
        user.setPassword("25d55ad283aa400af464c76d713c07ad");
        user.setExtraInfo("{}");
        userDao.save(user);//save方法不执行

        //第一种情况： throw new IOException("异常。。。");
        //第二种情况：
        int i = 1 / 0;//异常回滚
    }

    @Transactional(rollbackFor = Exception.class)
    public void rightTest002() throws Exception {
        int i = 1 / 0;//异常不往下走了
        this.rightTest001();//save方法不执行
    }

    @Transactional(rollbackFor = Exception.class)
    public void rightTest003() throws Exception {

        try {
            int i = 1 / 0; //异常了、catch住了、不往下走了
            EcommerceUser user = new EcommerceUser();
            user.setUsername("2022-12-04");
            user.setPassword("25d55ad283aa400af464c76d713c07ad");
            user.setExtraInfo("{}");
            userDao.save(user);//save方法不执行
        }catch (Exception e){
            log.error("异常：{}",e.getMessage());
        }
    }
}
